﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;

namespace CapaNegocio
{
    
    //BORRAR TIPO_EXAMENTIPO O NOMBRE_EXAMENTIPO SON IGUALES, MISMO TIPO DATO

    public class clsExamen
    {

        //tabla examen
        public int codigo_Examen                { get;set; }
        public string nombre_Examen             { get;set; }
        public string descripcion_Examen        { get;set; }
        public decimal precio_Examen            { get;set; }
        public int ExamenTipo_codigo_ExamenTipo { get;set; }

        //tabla examen tipo
        public int codigo_ExamenTipo            { get;set; } 
        public String tipo_ExamenTipo           { get;set; }
        public String nombre_ExamenTipo         { get;set; }
        public String descripcion_ExamenTipo    { get;set; }

        public clsBoleta clsBoleta
        {
            get
            {
                throw new System.NotImplementedException();
            }
            set
            {
            }
        }


        public DataTable mtdListarExamenesTodos()
        {
            clsConexion con                 = new clsConexion();           
            MySqlCommand command            = new MySqlCommand();
            DataTable ObjDatatable          = new DataTable();
            MySqlDataAdapter ObjDataAdapter = new MySqlDataAdapter();

            try
            {

                con.mtdConectar();
                command = con.conexion.CreateCommand();
                command.CommandText = "CALL spListarExamenes();"; //se modifico nombre de storeprocedure y el ORDER BY en el SP
                command.ExecuteNonQuery();

                ObjDataAdapter.SelectCommand = command;
                ObjDataAdapter.Fill(ObjDatatable);
                
                //con = null;


                con.mtdDesconectar();
                ObjDataAdapter = null;

                return ObjDatatable;
            }
            catch (Exception)
            {
                throw new Exception("System Error.");
            }
            //}

        }

        public DataTable mtdListarTiposExamenes()
        {
            clsConexion con                 = new clsConexion();            
            MySqlCommand command            = new MySqlCommand();
            DataTable ObjDatatable          = new DataTable();
            MySqlDataAdapter ObjDataAdapter = new MySqlDataAdapter();
            
            try
            {
                con.mtdConectar();
                command = con.conexion.CreateCommand();
                command.CommandText = "SELECT TIPO_EXAMENTIPO FROM EXAMENTIPO";
                command.ExecuteNonQuery();

                ObjDataAdapter.SelectCommand = command;
                ObjDataAdapter.Fill(ObjDatatable);

                //con = null;


                con.mtdDesconectar();
                ObjDataAdapter = null;

                return ObjDatatable;

            }
            catch (Exception)
            {
                throw new Exception("System Error.");
            }
        }

        public DataTable BuscarExamenes()
        {
            clsConexion con                 = new clsConexion();          
            MySqlCommand command            = new MySqlCommand();
            DataTable ObjDatatable          = new DataTable();
            MySqlDataAdapter ObjDataAdapter = new MySqlDataAdapter();
            String filtro                   = null;

            try
            {
                filtro = this.nombre_Examen;

                con.mtdConectar();
                command = con.conexion.CreateCommand();
                command.CommandText = "call spBuscarExamenes('" + filtro + "');"; //se modifico el ORDER BY en el SP
                command.ExecuteNonQuery();

                ObjDataAdapter.SelectCommand = command;
                ObjDataAdapter.Fill(ObjDatatable);

                con.mtdDesconectar();
                ObjDataAdapter = null;

                return ObjDatatable;
            }
            catch (Exception)
            {
                throw new Exception("System Error.");
            }
            //}

        }

        public bool mtdCrearExamen()
        {
            try
            {
                clsConexion con = new clsConexion();               
                MySqlCommand command;                
                
                con.mtdConectar();
                command = con.conexion.CreateCommand();
                command.CommandText = "call spCrearExamen('" + this.nombre_Examen + "','"+ this.descripcion_Examen +"',"+ this.precio_Examen +","+ this.ExamenTipo_codigo_ExamenTipo +");";
                command.ExecuteNonQuery();

                con.mtdDesconectar();

                
                return true;
            }
            catch (Exception)
            {
                System.Windows.Forms.MessageBox.Show("No se Pudo Agregar Examen");
                return false;                
            }
            
        }

        public bool mtdEditarExamen()
        {
            try
            {
                clsConexion con = new clsConexion();                
                MySqlCommand command;
                

                con.mtdConectar();
                command = con.conexion.CreateCommand();
                command.CommandText = "call spEditarExamen("+this.codigo_Examen+",'" + this.nombre_Examen + "','" + this.descripcion_Examen + "'," + this.precio_Examen + "," + this.ExamenTipo_codigo_ExamenTipo + ");";
                command.ExecuteNonQuery();

                con.mtdDesconectar();


                return true;
            }
            catch (Exception)
            {
                System.Windows.Forms.MessageBox.Show("No se Pudo Editar Examen");                
                return false;
            }
            
        }

        public bool mtdBuscarExamenCodigo()
        {

            clsConexion con = new clsConexion();            
            MySqlCommand command;
            MySqlDataReader Reader;
            int cod = 0;

            try
            {

                con.mtdConectar();
                command = con.conexion.CreateCommand();

                cod = this.codigo_Examen;

                if (cod != 0)
                {
                    command.CommandText = "CALL spBuscarExamenCodigo(" + cod + ");";
                    Reader = command.ExecuteReader();
                    while (Reader.Read())
                    {

                        this.codigo_Examen = Reader.GetInt16(0);
                        this.nombre_Examen = Reader.GetString(1);
                        this.descripcion_Examen = Reader.GetString(2);
                        this.precio_Examen = Reader.GetDecimal(3);
                        this.ExamenTipo_codigo_ExamenTipo = Reader.GetInt16(4);
                        //System.Windows.Forms.MessageBox.Show("Examen Encontrado");
                        return true;
                    }

                }
                else
                {
                    //System.Windows.Forms.MessageBox.Show("Examen NO Encontrado");
                    return false;
                }

                con.mtdDesconectar();
                //System.Windows.Forms.MessageBox.Show("Examen NO Encontrado");
                return false;
            }

            catch (Exception ex)
            {
                return false;
                throw ex;            
            }
        }
    }
}
